/**
 * @description 用户提交信息的 api 路由
 * @author LI
 */

const router = require('koa-router')()
const {
  register,
  getGoodTypeList,
  getGoodsInfoList,
  getGoodsPhotoInfo,
  getGoodsProductsInfo,
  getGoodsDecoratitemsInfo,
  linkUserDeduction,
  linkRegister,
  getSMSCode,
  updateUserPwd,
  getQQUserInfo,
  sendBoosSMS
} = require('../../controller/user')

router.prefix('/jihuide/user')

// 用户提交信息路由/注册
router.post('/register', async (ctx, next) => {
  const { userName, phone, area, address, remark } = ctx.request.body
  // 接下来交给控制层
  ctx.body = await register({
    userName,
    phone,
    area,
    address,
    remark
  })
})

// 用户提交信息路由/注册
router.post('/sendBoosSMS', async (ctx, next) => {
  const { phone } = ctx.request.body
  // 接下来交给控制层
  ctx.body = await sendBoosSMS(phone)
})

// 用户查看分类，每个分类的全部
router.get('/getGoodTypeInfo', async (ctx, next) => {
  let { typeName, page, limit } = ctx.request.query
  page = parseInt(page)
  limit = parseInt(limit)
  // 接下来把数据交给控制层
  const result = await getGoodTypeList({ typeName, page, limit })

  ctx.body = result
})

// 用户查看装修信息
router.get('/getGoodsInfo', async (ctx, next) => {
  let { id } = ctx.request.query
  id = parseInt(id)
  const result = await getGoodsInfoList(id)

  ctx.body = result
})

// 用户查看商品图片信息
router.get('/getGoodsPhotoInfo', async (ctx, next) => {
  let { id } = ctx.request.query
  id = parseInt(id)
  const result = await getGoodsPhotoInfo(id)

  ctx.body = result
})

// 用户查看商品产品信息
router.get('/getGoodsProductsInfo', async (ctx, next) => {
  let { id } = ctx.request.query
  id = parseInt(id)
  const result = await getGoodsProductsInfo(id)

  ctx.body = result
})

// 用户查看商品装修信息
router.get('/getGoodsDecoratitemsInfo', async (ctx, next) => {
  let { id } = ctx.request.query
  id = parseInt(id)
  const result = await getGoodsDecoratitemsInfo(id)

  ctx.body = result
})

// 用户点击产品链接进行相应店铺的扣费
router.post('/linkUserDeduction', async (ctx, next) => {
  const { companyAddress } = ctx.request.body
  const accessIp = ctx.request.ip
  console.error('---------ip:-- ' + accessIp)
  const result = await linkUserDeduction(companyAddress, accessIp)

  ctx.body = result
})

// 链接用户/获取验证码
router.post('/getSMSCode', async (ctx, next) => {
  const { phone } = ctx.request.body
  // 接下来交给控制层
  ctx.body = await getSMSCode(phone)
})

// 链接用户/注册
router.post('/linkRegister', async (ctx, next) => {
  let { account, pwd, code, typecode, typeLevel } = ctx.request.body
  account = String(account)
  code = Number(code)
  // 接下来交给控制层
  ctx.body = await linkRegister({
    account,
    pwd,
    code,
    typecode,
    typeLevel
  })
})

// 重置链接用户密码
router.get('/updateUserPwd', async (ctx, next) => {
  let { account, code, pwd } = ctx.request.query
  code = Number(code)
  // 接下来交给控制层
  ctx.body = await updateUserPwd({
    account,
    code,
    pwd,
  })
})

// 获取 qq 用户账号密码
router.get('/getQQUserInfo', async (ctx, next) => {
  const { code } = ctx.request.query
  console.error(code)
  // 接下来交给控制层
  ctx.body = await getQQUserInfo(code)
})

module.exports = router
